package com.dream.technology.service.impl;


import com.dream.technology.db.dao.RcFlowItemMapper;
import com.dream.technology.db.entity.RcFlowItem;
import com.dream.technology.db.entity.RcFlowItemQuery;
import com.dream.technology.service.RcFlowItemService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("rcFlowItemService")
public class RcFlowItemServiceImpl implements RcFlowItemService {

    @Autowired
    RcFlowItemMapper rcFlowItemMapper;

    @Override
    public int countByExample(RcFlowItemQuery example) {
        return rcFlowItemMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(RcFlowItemQuery example) {
        return rcFlowItemMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return rcFlowItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RcFlowItem record) {
        return rcFlowItemMapper.insert(record);
    }

    @Override
    public int insertSelective(RcFlowItem record) {
        return rcFlowItemMapper.insertSelective(record);
    }

    @Override
    public List<RcFlowItem> selectByExample(RcFlowItemQuery example) {
        return rcFlowItemMapper.selectByExample(example);
    }

    @Override
    public RcFlowItem selectByPrimaryKey(Long id) {
        return rcFlowItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(RcFlowItem record, RcFlowItemQuery example) {
        return rcFlowItemMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(RcFlowItem record, RcFlowItemQuery example) {
        return rcFlowItemMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(RcFlowItem record) {
        return rcFlowItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RcFlowItem record) {
        return rcFlowItemMapper.updateByPrimaryKey(record);
    }

    /**
     * 批量插入
     *
     * @param flowItems
     * @return
     */
    @Override
    public Integer insertList(List<RcFlowItem> flowItems) {
        return rcFlowItemMapper.insertList(flowItems);
    }


    /**
     * 复制元素
     *
     * @param id
     * @param newId
     * @return
     */
    @Override
    public String copy(Long id, Long newId) {
        List<RcFlowItem> rcFlowItems = selectByFlowId(id);
        if (CollectionUtils.isEmpty(rcFlowItems)) {
        	return null;
        }
        ArrayList<RcFlowItem> itemsArr = new ArrayList<>();
        rcFlowItems.forEach(rcFlowItem -> {
            rcFlowItem.setId(null);
            rcFlowItem.setFlowId(newId);
            itemsArr.add(rcFlowItem);
        });
        insertList(itemsArr);
        return null;
    }

    @Override
    public List<RcFlowItem> selectByFlowId(Long id) {
        RcFlowItemQuery query = new RcFlowItemQuery();
        RcFlowItemQuery.Criteria criteria = query.createCriteria();
        criteria.andFlowIdEqualTo(id);

        List<RcFlowItem> rcFlowItems = rcFlowItemMapper.selectByExample(query);
        return rcFlowItems;
    }

}
